﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

//using ZXing.QrCode;
//using ZXing.Common;

//using ZXing;
using System.Windows.Media.Imaging;
using ScannerDemo.common;
using System.Windows.Media;

namespace ScannerDemo
{
    public partial class GenerateUrl : PhoneApplicationPage
    {
        ApplicationBarIconButton appBarIconBtnQR = new ApplicationBarIconButton(new Uri("/icons/confirm.png", UriKind.Relative));
        ApplicationBarIconButton appBarIconBtnRelocation = new ApplicationBarIconButton(new Uri("/icons/scan icon.png", UriKind.Relative));
        DataValidator dataValidator = new DataValidator();
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                #region C#生成ApplicationBar已经相关的Button和事件.
                ApplicationBar = new ApplicationBar();

                //ApplicationBarIconButton appBarIconBtnQR = new ApplicationBarIconButton(new Uri("/icons/appbar.refresh.rest.png", UriKind.Relative));
                appBarIconBtnQR.Text = "生成";
                appBarIconBtnQR.Click += new EventHandler(ApplicationBar_Generation_Click);
                appBarIconBtnQR.IsEnabled = false;
                ApplicationBar.Buttons.Add(appBarIconBtnQR);



                appBarIconBtnRelocation.Text = "扫描";
                appBarIconBtnRelocation.Click += new EventHandler(ApplicationBar_StartPage_Click);
                ApplicationBar.Buttons.Add(appBarIconBtnRelocation);

                ApplicationBar.IsVisible = true;

                #endregion

            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
        public GenerateUrl()
        {
            InitializeComponent();
        }

        private void ApplicationBar_Generation_Click(object sender, EventArgs e)
        {
            string uriString = "" + textInput.Text;
               /*+ @"1
            2" + @"
            3";*/

            App.PublicText = uriString;
            NavigationService.Navigate(new Uri("/GenerateResult.xaml", UriKind.RelativeOrAbsolute));

       
      
            /*
            WriteableBitmap wbmi = new System.Windows.Media.Imaging.WriteableBitmap(width, height);
            
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int grayValue = bm.Array[y][x] & 0xff;
                    if (grayValue == 0)
                        wbmi.SetPixel(x, y, Color.FromArgb(255, 0, 0, 0));
                    else
                        wbmi.SetPixel(x, y, Color.FromArgb(255, 255, 255, 255));
                }
            }
            this.QrImg.Source = wbmi;
            */
             
        }



        private void ApplicationBar_StartPage_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }
        private void textInput_LostFocus(object sender, RoutedEventArgs e)
        {

            if (textInput.Text != "")
                checkAllData();
            else
            {
                appBarIconBtnQR.IsEnabled = false;
                //MessageBox.Show("请输入姓名!");
            }
        }
        private void checkAllData()
        {
            if ((dataValidator.IsValidUrl(textInput.Text)))
                appBarIconBtnQR.IsEnabled = true;
            else
                appBarIconBtnQR.IsEnabled = false;

        }

        private void textInput_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            checkAllData();
        }

        private void textInput_GotFocus(object sender, RoutedEventArgs e)
        {
            (sender as TextBox).Foreground = new SolidColorBrush(Colors.Black);
        }
    }
}